package study.domain;

import java.util.Arrays;

public class ArrayImpl extends MyArray{
    @Override
    public void query(int[] arr) {
        System.out.println(Arrays.toString(arr));
    }

    @Override
    public int indexQuery(int[] arr,int num) {
        int index=-1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==num){
                index = i;
                break;
            }
        }
        return index;
    }

    @Override
    public void reverse(int[] arr) {
        int[] newArr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i]=arr[arr.length-1-i];
        }
        query(newArr);
    }

    @Override
    public void delete(int[] arr,int index) {
        if (index<0 || index>= arr.length){
            System.out.println("索引错误！");
            return;
        }
        int[] newArr = new int[arr.length - 1];
        for (int i = 0; i < index; i++) {
            newArr[i]=arr[i];
        }
        for (int i = index; i < arr.length-1; i++) {
            newArr[i]=arr[i+1];
        }
        query(newArr);
    }

    @Override
    public void indexAdd(int[] arr,int index,int num) {
        if (index<0 || index>= arr.length){
            System.out.println("索引错误！");
            return;
        }
        int[] newArr = new int[arr.length + 1];

        for (int i = 0; i < index; i++) {
            newArr[i]=arr[i];
        }
        newArr[index]=num;

        for (int i = index+1; i < arr.length+1; i++) {
            newArr[i]=arr[i-1];
        }
        query(newArr);
    }

    @Override
    public boolean isFull(int[] arr) {
        int size=0;
        boolean flag= false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]!=0){
                size++;
            }
        }
        if (size==arr.length){
            flag=true;
        }
        return flag;
    }
}
